<?php
if(! function_exists('resizeImage')){
    /**
     * @param null $driver
     * @return mixed
     */
    // 原文件,新文件,宽度,高度,维持比例
    // 大图地址，新的图片地址名称，默认大小400*400
    function resizeImage($big_image_name, $width = 400, $height = 400, $resize = 1, $replace=''){
        $org_path = storage_path('app/public/').$big_image_name;
        if(!$org_path){
            return '';
        }
        if(strrpos($org_path, '.') === false){
            // $org_path = $_SERVER['DOCUMENT_ROOT'] . $replace;
        }else{
            // echo $org_path = public_path('upload/') . $big_image_name;die();
            if(! file_exists($org_path)){
                return 'not found';
            }
        }

        if($resize == 0){
            if($temp_img_type = @getimagesize($org_path)){
                @preg_match('/([a-z]+)$/', $temp_img_type['mime'], $tpn);
                $img_type = $tpn[1];
            }else{
                @preg_match('/.([a-z]+)$/', $org_path, $tpn);
                $img_type = $tpn[1];
            }
            $width = $temp_img_type[0];
            $height = $temp_img_type[1];
        }

        $dot_index = strrpos($big_image_name, '.');
        $file_extension = substr($big_image_name, $dot_index);
        $filename = substr($big_image_name, 0, $dot_index);
        if($resize == 0){
            $new_name = $filename . $file_extension;
        }else{
            $new_name = $filename . '_' . $width . 'x' . $height . $file_extension;
        }
        $new_name = public_path('upload/') . $new_name;
        if(! file_exists($new_name)){
            $tmp = explode('/', $filename);
            $obj_folder = $tmp[count($tmp)-2];
            Storage::disk($tmp[0].'_thumb')->makeDirectory($obj_folder);

            if($resize == 1){
                if($temp_img_type = @getimagesize($org_path)){
                    @preg_match('/([a-z]+)$/', $temp_img_type['mime'], $tpn);
                    $img_type = $tpn[1];
                }else{
                    @preg_match('/.([a-z]+)$/', $org_path, $tpn);
                    $img_type = $tpn[1];
                }
            }


            $max_width = $temp_img_type[0];
            $max_height = $temp_img_type[1];
            $all_type = array(
                "jpg" => array("create" => "imagecreatefromjpeg", "output" => "imagejpeg", "exn" => ".jpg"),
                "gif" => array("create" => "imagecreatefromgif", "output" => "imagegif", "exn" => ".gif"),
                "jpeg" => array("create" => "imagecreatefromjpeg", "output" => "imagejpeg", "exn" => ".jpg"),
                "png" => array("create" => "imagecreatefrompng", "output" => "imagepng", "exn" => ".png"),
                "wbmp" => array("create" => "imagecreatefromwbmp", "output" => "image2wbmp", "exn" => ".wbmp")
            );
            $func_create = $all_type[$img_type]['create'];
            // 输出
            $func_output = $all_type[$img_type]['output'];
            
            $func_exname = $all_type[$img_type]['exn'];
            if(($func_exname == '.gif' || $func_exname == '.png') && !function_exists($func_output)){
                $func_output = 'imagejpeg';
                $func_exname = '.jpg';
            }

            $src_image = @$func_create($org_path);
            $src_w = @imagesx($src_image);
            $src_h = @imagesy($src_image);
            $dst_width = $width;
            $dst_height = $height;
            if(function_exists("imagecopyresampled")){

                $dst_image = @imagecreatetruecolor($dst_width, $dst_height);
                @imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_width, $dst_height, $src_w, $src_h);
            }else{

                $dst_image = @imagecreate($dst_width, $dst_height);
                @imagecopyresized($dst_image, $src_image, 0, 0, 0, 0, $dst_width, $dst_height, $src_w, $src_h);
            }
            if($func_output == 'imagejpeg'){
                @$func_output($dst_image, $new_name, 90);
            }else{
                @$func_output($dst_image, $new_name);
            }
            @imagedestroy($src_image);
            @imagedestroy($dst_image);
        }
        return substr($new_name, strlen(public_path('')));
    }
}